import requests
import re
from urllib3.exceptions import InsecureRequestWarning
import datetime

# 禁用安全请求警告
Platform = "神书"
today = datetime.date.today()  # 今天
yester = today - datetime.timedelta(days=1)
yesterday = str(yester).replace('-', '')


def shenshu():
    try:
        requests.urllib3.disable_warnings(InsecureRequestWarning)
        r = requests.session()
        date = {
            "_csrf": "X1hhc3c4ckppKiAgQlw5ADoNUEYQUhAYaTFVIDVfGmcVHS8/AFo.CA==",
            "LoginForm[username]": "",
            "LoginForm[password]": "",
            "submit_btn": "登录"
        }
        cookie_dict = {
            "Cookie": "SESSIONID=oath6uon4vgt8nq435hgu6e43g; "
                      "_csrf=75088da3664e0d1113edeae92634996f73b0e3ec6a48de"
                      "3007d649c25407be15a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csr"
                      "f%22%3Bi%3A1%3Bs%3A32%3A%226rAS5dKJeU15gjbR6i4SBgh-JENLwbLB%22%3B%7D"}
        cookies = requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)
        r.cookies = cookies
        header = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Content-Length": "194",
            "Content-Type": "application/x-www-form-urlencoded",
            "Host": "xs.cd414.com",
            "Origin": "https://xs.cd414.com",
            "Referer": "https://xs.cd414.com/index.php?r=main%2Flogin",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
        }
        rr = r.post(url="https://xs.cd414.com/index.php?r=main%2Flogin", data=date, headers=header, verify=False,
                    timeout=15)
        aa = r.get(url="https://xs.cd414.com/index.php?r=statistics%2Forder-statis", verify=False, timeout=15)
        html = aa.text.replace('\r', '').replace('\t', '').replace('\n', '')
        date_time = yesterday
        Amount_share = re.findall('<td>{0}</td>\s+<td><b>￥(.*?)</b></td>'.format(str(yester)), html)
        Amount_share = round(float(Amount_share[0]) * 0.9, 2)
        return date_time, Amount_share, Platform
    except Exception:
        print("获取 神书平台 数据失败")
        return '', 'None', Platform
    except ConnectionError:
        print("获取 神书 数据 超时")
        return '', 'None', Platform